app.factory(ꞌApiServiceꞌ)   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
/*
2
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
3
 *
4
 * @author Julius Härtl <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *  
8
 *  This program is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU Affero General Public License as
10
 *  published by the Free Software Foundation, either version 3 of the
11
 *  License, or (at your option) any later version.
12
 *  
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU Affero General Public License for more details.
17
 *  
18
 *  You should have received a copy of the GNU Affero General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *  
21
 */
22
23
app.factory('ApiService', function($http, $q){
24
    var ApiService = function(http, endpoint) {
25
        this.endpoint = endpoint;
26
        this.baseUrl = OC.generateUrl('/apps/deck/' + endpoint);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
        this.http = http;
28
        this.q = $q;
29
        this.data = {};
30
        this.id = null;
31
        this.sorted = [];
32
    };
33
34
    ApiService.prototype.fetchAll = function(){
35
        var deferred = $q.defer();
36
        var self = this;
37
        $http.get(this.baseUrl).then(function (response) {
38
            var objects = response.data;
39
            objects.forEach(function (obj) {
40
                self.data[obj.id] = obj;
41
            });
42
            deferred.resolve(self.data);
43
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
44
            deferred.reject('Fetching ' + self.endpoint + ' failed');
45
        });
46
        return deferred.promise;
47
    };
48
49
    ApiService.prototype.fetchOne = function (id) {
50
51
        this.id = id;
52
        var deferred = $q.defer();
53
54
        if(id===undefined) {
55
            return deferred.promise;
56
        }
57
58
        var self = this;
59
        $http.get(this.baseUrl + '/' + id).then(function (response) {
60
            data = response.data;
0 ignored issues
show
Bug introduced by
The variable data seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.data.
Loading history...
61
            if(self.data[data.id]===undefined) {
62
                self.data[data.id] = response.data;
63
            }
64
            $.each(response.data, function(key, value) {
65
                self.data[data.id][key] = value;
66
            });
67
            deferred.resolve(response.data);
68
69
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
70
            deferred.reject('Fetching ' + self.endpoint + ' failed');
71
        });
72
        return deferred.promise;
73
    };
74
75
    ApiService.prototype.create = function (entity) {
76
        var deferred = $q.defer();
77
        var self = this;
78
        $http.post(this.baseUrl, entity).then(function (response) {
79
            self.add(response.data);
80
            deferred.resolve(response.data);
81
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
82
            deferred.reject('Fetching' + self.endpoint + ' failed');
83
        });
84
        return deferred.promise;
85
    };
86
87
    ApiService.prototype.update = function (entity) {
88
        var deferred = $q.defer();
89
        var self = this;
90
        $http.put(this.baseUrl + '/' + entity.id, entity).then(function (response) {
91
            self.add(response.data);
92
            deferred.resolve(response.data);
93
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
94
            deferred.reject('Updating ' + self.endpoint + ' failed');
95
        });
96
        return deferred.promise;
97
98
    };
99
100
    ApiService.prototype.delete = function (id) {
101
        var deferred = $q.defer();
102
        var self = this;
103
104
        $http.delete(this.baseUrl + '/' + id).then(function (response) {
105
            self.remove(id);
106
            deferred.resolve(response.data);
107
108
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
109
            deferred.reject('Deleting ' + self.endpoint + ' failed');
110
        });
111
        return deferred.promise;
112
113
    };
114
    
115
116
    // methods for managing data
117
    ApiService.prototype.clear = function() {
118
        this.data = {};
119
    };
120
    ApiService.prototype.add = function (entity) {
121
        var element = this.data[entity.id];
122
        if(element===undefined) {
123
            this.data[entity.id] = entity;
124
        } else {
125
            Object.keys(entity).forEach(function (key) {
126
                element[key] = entity[key];
127
                if(element[key]!==null)
128
                    element[key].status = {};
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
129
            });
130
        }
131
    };
132
    ApiService.prototype.remove = function(id) {
133
        if (this.data[id] !== undefined) {
134
            delete this.data[id];
135
        }
136
    };
137
    ApiService.prototype.addAll = function (entities) {
138
        var self = this;
139
        angular.forEach(entities, function(entity) {
140
            self.add(entity);
141
        });
142
    };
143
144
    ApiService.prototype.getCurrent = function () {
145
        return this.data[this.id];
146
    };
147
148
    ApiService.prototype.getData = function() {
149
        return $.map(this.data, function(value, index) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
150
            return [value];
151
        });
152
    };
153
154
    ApiService.prototype.getAll = function () {
155
        return this.data;
156
    };
157
158
    ApiService.prototype.getName = function() {
159
        var funcNameRegex = /function (.{1,})\(/;
160
        var results = (funcNameRegex).exec((this).constructor.toString());
161
        return (results && results.length > 1) ? results[1] : "";
162
    };
163
164
    return ApiService;
165
166
});
167